package com.xxx.yuedong.rabbitmq;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * @author HG
 * @version 1.0
 **/
@Component
@Slf4j
public class MyAckReceiver implements ChannelAwareMessageListener {
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            byte[] body = message.getBody();
            String msg = new String(body, StandardCharsets.UTF_8);
            log.debug("MyAckReceiver message: " + msg);
            log.debug("消费的主题消息来自队列：" + message.getMessageProperties().getConsumerQueue());
            channel.basicAck(deliveryTag, true);
            //第二个参数，手动确认可以被批处理，当该参数为true时，则可以一次性确认delivery_tag小于等于传入值的所有消息
            //channel.basicAck(deliveryTag, true, true)第三个参数，true会重新放入队列，所以应该根据你的业务逻辑进行判断什么时候拒绝
        } catch (Exception e) {
            channel.basicReject(deliveryTag, false);//拒绝消费
            e.printStackTrace();
        }
    }
}
